﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Live;
using System.Text;
using System.IO;

namespace DemoShoppingList
{
    public partial class LiveSignIn : PhoneApplicationPage
    {
        private LiveConnectSession LiveSession;
        private LiveConnectClient client;

        private string fileBody;
        private string fileName;

        public LiveSignIn()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.ContainsKey("body"))
            {
                fileBody = NavigationContext.QueryString["body"].ToString();
                                
                NavigationService.RemoveBackEntry();
            }
            if (NavigationContext.QueryString.ContainsKey("fileName"))
            {
                fileName = NavigationContext.QueryString["fileName"].ToString();
            }
        }

        private void signInButton1_SessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e)
        {
            if (e.Status == LiveConnectSessionStatus.Connected)
            {
                client = new LiveConnectClient(e.Session);
                LiveSession = e.Session;
                textBlock1.Text = "You are signed in!";
                button1.IsEnabled = true;
            }
            else
            {
                client = null;
            }
        }

        private void ApplicationBarIconButton_Back(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml?back=0", UriKind.Relative));
        }
                
        private void button1_Transmit(object sender, RoutedEventArgs e)
        {
            byte[] byteArray = Encoding.Unicode.GetBytes(fileBody);
            MemoryStream fileStream = new MemoryStream(byteArray);

            LiveConnectClient uploadClient = new LiveConnectClient(LiveSession);
            uploadClient.UploadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(uploadClient_UploadCompleted);
            uploadClient.UploadAsync("me/skydrive", fileName + ".txt", fileStream);

            textBlock2.Visibility = System.Windows.Visibility.Visible;
        }
        
        void uploadClient_UploadCompleted(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("Your data saved to the SkyDrive :)", "All Done!", MessageBoxButton.OK);
            }
            NavigationService.Navigate(new Uri("/MainPage.xaml?back=0", UriKind.Relative));
        }
    }
}